package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.entity.my.user.KqUser;
import com.example.ymm.modules1.mapper.my.user.KqUserMapper;
import com.example.ymm.modules1.query.kq.KqUserQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.kq.ImportKqUserDto;
import com.example.ymm.modules1.service.dto.heng.kq.ViewKqUser;
import com.example.ymm.modules1.service.hl.kq.KqRuleService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.modules1.service.hl.kq.KqDeptService;
import com.example.ymm.modules1.service.hl.kq.KqUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqUserServiceImpl extends BaseServiceImpl<KqUserMapper, KqUser> implements KqUserService {

    @Autowired
    KqDeptService kqDeptService;

    @Autowired
    DbUserService dbUserService;

    @Autowired
    KqRuleService kqRuleService;

    @Override
    public TableList<ViewKqUser> list(KqUserQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<ViewKqUser> arrs = userViewList(criteria);
        PageInfo<ViewKqUser> pageInfo = new PageInfo<>(arrs);
        TableList<ViewKqUser> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<KqUser> queryAll(KqUserQueryCriteria criteria) {
        List<KqUser> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqUser.class, criteria));
        return result;
    }

    @Override
    public List<String> importKqUser(ImportKqUserDto dto) {
        String groupId = dto.getGroupId();
        // 转换JSONArray为List
        List<ViewKqUser> list=dto.getList();
        List<String> result=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ViewKqUser viewKqUser = list.get(i);
            if(UtilValidate.isEmpty(viewKqUser.getUserName())){
                result.add("第"+(i+1)+"行 姓名不能为空");
            }
            if(UtilValidate.isNotEmpty(viewKqUser.getIzLianBanTxt())){
                if(!"是".equals(viewKqUser.getIzLianBanTxt())
                        &&!"否".equals(viewKqUser.getIzLianBanTxt())){
                    result.add("第"+(i+1)+"行 是否连班请填'是'或'否'，不要填:"+viewKqUser.getIzLianBanTxt());
                }else{
                    if("是".equals(viewKqUser.getIzLianBanTxt())){
                        viewKqUser.setIzLianBan(1);
                    }
                    if("否".equals(viewKqUser.getIzLianBanTxt())){
                        viewKqUser.setIzLianBan(0);
                    }

                }
            }
            viewKqUser.setUserName(viewKqUser.getUserName().trim());
            DbUser dbUser = dbUserService.findByUserName(viewKqUser.getUserName());
            if(UtilValidate.isEmpty(dbUser)){
                result.add("第"+(i+1)+"行 "+viewKqUser.getUserName()+" 查无此人请联系人事核对是否邀请到钉钉的组织架构中，或者检查是否输入正确!");
            }else {
                viewKqUser.setUserId(dbUser.getUserId());
                viewKqUser.setUserName(dbUser.getUserName());
                viewKqUser.setKqGroupId(groupId);
            }
        }
        if(UtilValidate.isNotEmpty(result)){
            return result;
        }
        for (int i = 0; i < list.size(); i++) {
            ViewKqUser viewKqUser = list.get(i);
            KqUser kqUser = new KqUser();
            BeanUtils.copyProperties(viewKqUser, kqUser);
            saveOrUpdate(kqUser);
        }
        return result;
    }


    @Override
    public List<ViewKqUser> userViewList(KqUserQueryCriteria criteria) {
        List<ViewKqUser> viewKqUsers = baseMapper.userViewList(criteria);
        viewKqUsers.forEach(viewKqUser -> {
           if(UtilValidate.isNotEmpty(viewKqUser.getIzLianBan())){
               if(viewKqUser.getIzLianBan().equals(1)){
                   viewKqUser.setIzLianBanTxt("连班");
               }else if(viewKqUser.getIzLianBan().equals(0)){
                   viewKqUser.setIzLianBanTxt("不连班");
               }
           }
        });
        return viewKqUsers;
    }
}
